package view.macrogui
{
	import events.WorldEvent;
	import flash.display.Shader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import model.macro.Unit;
	
	/**
	 */
	public class UnitItem extends Sprite
	{
		private const FORMAT:TextFormat = new TextFormat("Helvetica", 12, 0xFFFFFF);
		
		static public const HEIGHT:int = 32;
		static private const BG_MOVED_SELECTED:Number = 0xFF2222;
		static private const BG_MOVED_HOVER:Number = 0xDD1111;
		static private const BG_MOVED:Number = 0xAA0000;
		static private const BG_SELECTED:Number = 0x777777;
		static private const BG_HOVER:Number = 0x555555;
		static private const BG_NORMAL:Number = 0x444444;
		
		public var unit:Unit;
		private var text_field:TextField;
		private var bars:Shape;
		private var selected:Boolean;
		private var _bg:int;
		private var hover:Boolean;
		
		/**
		 *
		 */
		public function UnitItem()
		{
			selected = false;
			
			text_field = new TextField();
			text_field.defaultTextFormat = FORMAT;
			text_field.selectable = false;
			text_field.x = 1;
			text_field.y = -1;
			addChild(text_field);
			
			bars = new Shape();
			bars.x = 6;
			bars.y = 20;
			addChild(bars);
			
			redraw();
			
			addEventListener(MouseEvent.CLICK, onClick);
			addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void
				{
					hover = true;
					redraw();
				});
			addEventListener(MouseEvent.MOUSE_OUT, function(e:Event):void
				{
					hover = false;
					redraw();
				});
		}
		
		private function redraw():void
		{
			if (unit == null)
				return;
			
			if (unit.moves_remaining <= 0)
			{
				if (selected)
					bg = BG_MOVED_SELECTED;
				else if (hover)
					bg = BG_MOVED_HOVER;
				else
					bg = BG_MOVED;
			}
			else
			{
				if (selected)
					bg = BG_SELECTED;
				else if (hover)
					bg = BG_HOVER;
				else
					bg = BG_NORMAL;
			}
			
			// bars
			var w:int = SideBar.WIDTH - SideBar.PADDING * 2;
			bars.graphics.beginFill(0x000000);
			bars.graphics.drawRect(0, 0, w - 12, 4);
			bars.graphics.endFill();
			bars.graphics.beginFill(0xFFAA00);
			bars.graphics.drawRect(0, 0, (w - 12) / (Math.random() * 3.0 + 1), 4);
			bars.graphics.endFill();
		}
		
		private function set bg(color:int):void
		{
			_bg = color;
			var w:int = SideBar.WIDTH - SideBar.PADDING * 2;
			graphics.clear();
			graphics.beginFill(color);
			graphics.drawRoundRect(0, 0, w, HEIGHT, 8);
			graphics.endFill();
			
			graphics.lineStyle(0, 0xFFFFFF);
			graphics.beginFill(unit.owner.color);
			graphics.drawRect(w - 20, 2, 16, 10);
			graphics.endFill();
		}
		
		private function get bg():int
		{
			return _bg;
		}
		
		private function onClick(e:MouseEvent):void
		{
			if (e.shiftKey)
			{
				if (selected)
					dispatchEvent(new WorldEvent(WorldEvent.UNIT_DESELECTED, null, unit));
				else
					dispatchEvent(new WorldEvent(WorldEvent.UNIT_SELECTED, null, unit));
			}
			else
			{
				dispatchEvent(new WorldEvent(WorldEvent.ALL_UNITS_DESELECTED, null));
				dispatchEvent(new WorldEvent(WorldEvent.UNIT_SELECTED, null, unit));
			}
		}
		
		public function update(unit:Unit):void
		{
			this.unit = unit;
			text_field.text = unit.name;
			text_field.width = text_field.textWidth + 5;
			text_field.height = text_field.textHeight + 4;
			redraw();
		}
		
		public function select():void
		{
			selected = true;
			redraw();
		}
		
		public function deselect():void
		{
			selected = false;
			redraw();
		}
	}

}